package com.dsg.controller.oil;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.account.DsgAccountTrade;
import com.dsg.entity.oil.DsgOilData;
import com.dsg.entity.oil.DsgOilDataDetail;
import com.dsg.service.oil.IDsgOilDataDetailService;
import com.dsg.util.ExcelIteratorReader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 入油数据明细 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/oil/dsgOilDataDetail")
@Api(description = "入油数据明细")
@Slf4j
public class DsgOilDataDetailController extends BaseController {

    @Autowired
    private IDsgOilDataDetailService dsgOilDataDetailService;

    /**
     * 查询列表（分页）
     *
     * @param dsgOilDataDetail
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgOilDataDetail dsgOilDataDetail) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("A.TRANS_DATE");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgOilDataDetail.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = dsgOilDataDetailService.selectByList(page, dsgOilDataDetail);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 查询单条数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}/view")
    @ApiOperation("查询单条数据")
    public AjaxResult view(@PathVariable String id) {
        DsgOilDataDetail dsgOilDataDetail = dsgOilDataDetailService.getById(id);
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilDataDetail);
        return ajaxResult;
    }

    /**
     * 新增
     *
     * @param dsgOilDataDetail
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgOilDataDetail dsgOilDataDetail) {
        dsgOilDataDetail.setOid(UUIDGenerator.getUUID());
        boolean flag = dsgOilDataDetailService.save(dsgOilDataDetail);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilDataDetail);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param dsgOilDataDetail
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgOilDataDetail dsgOilDataDetail) {
        AjaxResult ajaxResult;
        boolean flag = dsgOilDataDetailService.updateById(dsgOilDataDetail);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilDataDetail);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = dsgOilDataDetailService.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 上載
     *
     * @return
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation("上載")
    public AjaxResult importExcel(DsgOilData dsgOilData,
                                  @ApiParam(value = "file", name = "file", required = true) @RequestParam("file") MultipartFile file) throws Exception {
        if (StringUtil.isEmpty(dsgOilData.getOilCom())) {
            return setErrorJson("公司编码不能為空!");
        }
        if (dsgOilData.getDataType() == null) {
            return setErrorJson("卡類型不能為空!");
        }
        File excelFile = ExcelIteratorReader.multipartToFile(file);
        dsgOilDataDetailService.importExcel(excelFile, dsgOilData);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    /**
     * 接受数据
     *
     * @param idList   主键
     * @param isAccept 1：接受 2：拒绝
     * @return
     */
    @PutMapping(value = "/updateIsAccept")
    @ApiOperation("接受数据")
    public AjaxResult updateIsAccept(@ApiParam(value = "id以,号分隔", name = "idList", required = true) @RequestParam("idList") String idList,
                                     @ApiParam(value = "是否接受 1：接受 2：拒绝", name = "isAccept", required = true) @RequestParam("isAccept") String isAccept) {
        dsgOilDataDetailService.updateIsAccept(idList, isAccept);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    /**
     * 查询重复数据
     *
     * @param dsgOilDataDetail
     * @return
     */
    @GetMapping(value = "/getRepeatList")
    @ApiOperation("查询重复数据")
    public AjaxResult getRepeatList(DsgOilDataDetail dsgOilDataDetail) {
        dsgOilDataDetail.setEntCode(SingletonLoginUtils.getSystemEntCode());
        List<DsgOilDataDetail> dsgOilDataDetailList = dsgOilDataDetailService.selectRepeatList(dsgOilDataDetail);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilDataDetailList);
        return ajaxResult;
    }
    /**
     * 費用調整分頁查詢
     *
     * @param dsgAccountTrade
     * @return
     */
    @GetMapping(value = "/listAdj")
    @ApiOperation("費用調整分頁查詢")
    public AjaxResult listAdj(DsgAccountTrade dsgAccountTrade) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("a.CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgAccountTrade.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = dsgOilDataDetailService.selectListAdj(page, dsgAccountTrade);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }
}
